<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/css/custom.form.css">
</head>
<style>
    .img {
        position: relative;
        display: inline-block;
    }

    .img-del {
        position: absolute;
        left: 60px;
        top: 5px;
    }
</style>
<body>
<div class="panel panel-default operation" hidden>
    <div class="panel-heading title"></div>
    <div class="layui-card-body">
        <form class="layui-form " action="" lay-filter="info" style="width: 700px;margin-top: 10px">
            <input name="id" hidden/>
            <div class="layui-upload">
                <button type="button" class="layui-btn" id="imgUP">多图片上传</button>
                <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                    预览图：
                    <div class="layui-upload-list" id="img-box">
                        <!--<div id="a1" class="img">-->
                        <!--<input hidden/>-->
                        <!--<img id="b1" src="/uploadFile/2020/07/13/62ec8eadfe88c76c1c097d4919bfb8f8.jpg" height="100"-->
                        <!--width="100"/>-->
                        <!--<button type="button" class="del layui-btn layui-btn-danger layui-btn-xs">删除</button>-->
                        <!--</div>-->
                        <!--<div id="a2" class="img">-->
                        <!--<input hidden/>-->
                        <!--<img id="b2" src="/uploadFile/2020/07/13/62ec8eadfe88c76c1c097d4919bfb8f8.jpg" height="100"-->
                        <!--width="100"/>-->
                        <!--<button type="button" class="del layui-btn layui-btn-danger layui-btn-xs">删除</button>-->
                        <!--</div>-->
                    </div>
                </blockquote>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button id="img-submit" class="layui-btn">保存</button>
                    <button class="layui-btn layui-btn-primary" id="btn_cancel">返回</button>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="table_div">
    <div id="searchParam" shiro:hasPermission="sysFile:list">
        <div class="layui-form-item">
            <div class="layui-input-inline">
                <input type="text" id="key" class="layui-input" autocomplete="off" placeholder="请输入">
            </div>

            <div class="layui-input-inline ">
                <button class="layui-btn" onclick="search()" id="search">查询</button>
                <button class="layui-btn" id="export">导出全部</button>
            </div>
        </div>

    </div>
    <table class="layui-table" id="showTable" lay-filter="showTable"></table>
</div>
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">上传</button>
        <button class="layui-btn layui-btn-sm" lay-event="batchDeleted" shiro:hasPermission="sysFile:delete">删除</button>
    </div>
</script>
<script type="text/html" id="tool">
    <a class="layui-btn layui-btn-xs" lay-event="show">查看</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" shiro:hasPermission="sysFile:delete">删除</a>
</script>

</body>
</html>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script>
    //获取token
    var token = CoreUtil.getData("access_token");
    //地址栏转义token中的#号
    var tokenQuery = token.replace("#", "%23");
    var tableIns1;
    var table = layui.table;
    var form = layui.form;
    var layer = layui.layer;
    var $ = jQuery = layui.jquery;
    var laydate = layui.laydate;
    var upload = layui.upload;
    layui.use(['table', 'layer', 'laydate', 'upload'], function () {

        //加载table
        tableIns1 = table.render({
            elem: '#showTable'
            , contentType: 'application/json'
            , headers: {"authorization": token}
            , page: true //开启分页
            , url: '/sysFile/listByPage' //数据接口
            , method: 'POST'
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
                    "data": CoreUtil.isEmpty(res.data) ? null : res.data.records //解析数据列表
                }
            }
            , cols: [
                [
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'id', title: 'id', sort: true, width: 120},
                    {field: 'name', title: '文件名', sort: true},
                    {field: 'url', title: '文件地址', sort: true},
                    {field: 'suffix', title: '文件后缀', sort: true, width: 120},
                    {field: 'createTime', title: '创建时间', sort: true},
                    {width: 120, toolbar: "#tool", title: '操作'}
                ]
            ]
            , toolbar: '#toolbar'
        });


        //表头工具
        table.on('toolbar(showTable)', function (obj) {
            switch (obj.event) {
                case 'batchDeleted':
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    if (data.length == 0) {
                        layer.msg("请选择要批量删除的列");
                    } else {
                        var ids = [];
                        $(data).each(function (index, item) {
                            ids.push(item.id);
                        });
                        tipDialog(ids);
                    }
                    break;
                case 'add':
                    $(".table_div").hide();
                    $(".operation").show();
                    $(".title").html("新增");
                    $(".operation input[name=id]").val("");
                    $(".operation input[name=name]").val("");
                    $(".operation input[name=url]").val("");
                    $(".operation input[name=suffix]").val("");
                    $(".operation input[name=createTime]").val("");
                    break;
            }
            ;
        });
        //列操作
        table.on('tool(showTable)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    var ids = [];
                    ids.push(data.id);
                    tipDialog(ids);
                    break;
                case 'show':
                    var img = new Image();
                    img.src = data.url;
                    img.onload = function () {
                        var height = img.height;
                        var width = img.width;
                        var scaleWH = width / height;
                        var bigH = 600;
                        var bigW = scaleWH * bigH;
                        if (bigW > 900) {
                            bigW = 900;
                            bigH = bigW / scaleWH;
                        }
                        layer.open({
                            type: 1,
                            title: false,
                            closeBtn: 1,
                            shadeClose: true,
                            area: [bigW + 'px', bigH + 'px'], //宽高
                            content: '<img width=' + bigW + ' height=' + bigH + ' src="' + img.src + '" />'
                        });
                    }
                    break;
            }
        });


        //删除
        var tipDialog = function (ids) {
            layer.open({
                content: "确定要删除么?",
                yes: function (index, layero) {
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    CoreUtil.sendDelete("/sysFile/delete", ids, function (res) {
                        layer.msg(res.msg, {time: 1000}, function () {
                            search();
                        });
                    });
                }
            });
        };

        //返回
        $("#btn_cancel").click(function () {
            $(".table_div").show();
            $(".operation").hide();
            return false;
        });

        //监听保存
        // form.on('submit(submit)', function (data) {
        //     if (data.field.id === undefined || data.field.id === null || data.field.id === "") {
        //
        //     } else {
        //         CoreUtil.sendPut("/sysFile/update", data.field, function (res) {
        //             $(".table_div").show();
        //             $(".operation").hide();
        //             search();
        //         });
        //     }
        //
        //     return false;
        // });

        var files;
        //多图片上传
        upload.render({
            elem: '#imgUP'
            , url: '/sysFile/uploadImg' //改成您自己的上传接口
            , headers: {"authorization": token}
            , acceptMime: 'image/*'
            , bindAction: '#img-submit'
            , multiple: true   // 开启多文件上传
            , size: 1024        // 最大允许上传的文件大小  单位 KB
            , auto: false //选择文件后不自动上传
            , number: 6    //  同时上传文件的最大个数
            , choose: function (obj) {
                files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                obj.preview(function (index, file, result) {
                    var imgbox = 'img-' + index;
                    var html = '' +
                        '<div id="' + imgbox + '" class="img">\n' +
                        '     <img id="' + index + '" src="' + result + '" height="100" width="100"/>\n' +
                        '     <button type="button" class="img-del layui-btn layui-btn-danger layui-btn-xs">删除</button>\n' +
                        '</div>';
                    $("#img-box").append(html);
                    $(document).on('click', '.img-del', function () {
                        var my = $(this);
                        my.parent()[0].remove();
                        delete files[index]
                    });

                    layer.photos({
                        photos: '#' + imgbox
                        , anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
                    });

                    // $(document).on('click', '#' + index, function () {
                    //
                    // });
                });
            }
            , before: function (obj) {

            }
            , done: function (res) {
                //上传完毕
                tableIns1.reload();
                $(".table_div").show();
                $(".operation").hide();
            }
            , error: function (index, upload) {

            }
        });
    });

    //执行查询
    function search() {
        //这里以搜索为例
        tableIns1.reload({
            where: { //设定异步数据接口的额外参数，任意设
                key: $("#key").val()
            }
            , page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    };
</script>